﻿package se.penselnsvag.bookdb.tech.collection{


/**
* Simple HashMap implementation, build up on Object
*
* Author:mattias.anstrand@loveone.se
* Date: 2006-02-13
* Version: 1.1 Stricter than ever
* History: -
*/


public dynamic class HashMap
{	
	private var o:Object;
	public function HashMap()
	{
		o = new Object();
	}
	
	
	/** 
	* get(), get Function.
	* 
	* @param key to be used to search for object.
	* @return Object mapped to key.
	**/
	public function get(key:Hashable):Object 
	{
		return o[key.hashCode()];
	}
	/** 
	* getKeyByValue(), get Function.
	* 
	* @param value to be used to search for key.
	* @return Object mapped to value.
	**/
	/*public function getKeyByValue(value:Object):Object 
	{
			for(var i in o)
			{
				if(o[i] == value)
					return i;
			}
			
		return null;
	}
	*/
	/**
	* size(), 
	* 
	* @return Number of objects in Map.
	*/
	public function size():Number {
		var s:Number = 0;
		for(var i: String in o){
			s++;
		}
			
		return s;
	}
	
	/**
	* remove(), Removes object mapped to param key.
	*
	* @param key of object to be removed.
	* @return deleted object.
	*/
	public function remove(key:Hashable) : Object {
		
		var returnObject : Object = o[key.hashCode()];
		
		delete(o[key.hashCode()]);
		
		return returnObject;
	}
	
	/**
	* clear(), cleas the map. 
	*/
	public function clear():void {
		for(var i:String in o){
			delete(o[i]);
		}
	}
	
	/*
	* put(), Stores one object in the Map.
	*
	* @param key to be used as key.
	* @param value to be stored.
	*/
	public function put(key:Hashable, value:Object) : Object
	{
		var old:Object = o[key.hashCode()];
		o[key.hashCode()] = value;
		return old;
	}


	/**
	* getKeys(), Returns all keys currently in Map.
	*
	* @return Array of keys.
	*/
	public function getKeys() : Array 
	{
		var a:Array = new Array();
		for(var i:String in o){
			a.push(new HashableString(i));
		}
		
		return a;
	}
	
	/**
	* getValues(), Returns all values currently in Map.
	* 
	* @return Array of values.
	*/
	public function getValues() : Array
	{
		var a:Array = new Array();
		for(var i:String in o){
			a.push(o[i]);
		}
		
		return a;
	}
	
	public function toString():String {
		var s:String = "";
		var keys:Array = getKeys();
		for(var i:int = 0;i<keys.length;i++) {
			var k:Hashable = keys[i];
			s=s+k+"->"+o[k.hashCode()]+"\n";
		}
		
		return s;
	}
}
}